package com.wang.demo.service.impl;


import com.wang.bean.PageResponse;
import com.wang.demo.convert.PageSysUserPoConvert;
import com.wang.demo.convert.SysUserPoConvert;
import com.wang.demo.entity.dto.PageSysUserDto;
import com.wang.demo.entity.dto.SysUserDto;
import com.wang.demo.entity.po.SysUserPo;
import com.wang.demo.dao.SysUserDao;
import com.wang.demo.service.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysUserPo queryById(Long id) {
        return this.sysUserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param pageSysUserDto 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResponse<SysUserPo> queryByPage(PageSysUserDto pageSysUserDto) {
        SysUserPo sysUserPo = PageSysUserPoConvert.INSTANCE.convertDtoToPo(pageSysUserDto);
        PageResponse<SysUserPo> pageResponse = new PageResponse<>();
        pageResponse.setCurrent(Math.toIntExact(pageSysUserDto.getCurrent()));
        pageResponse.setPageSize(Math.toIntExact(pageSysUserDto.getSize()));
        Long pageStart = Long.valueOf(pageResponse.getStart());
        long total = this.sysUserDao.count(sysUserPo);
        List<SysUserPo> sysUserList = this.sysUserDao.queryAllByLimit(sysUserPo, pageStart, pageSysUserDto.getSize());
        pageResponse.setTotal((int) total);
        pageResponse.setResult(sysUserList);
        return pageResponse;
    }

    /**
     * 新增数据
     *
     * @param sysUserDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserPo insert(SysUserDto sysUserDto) {
        SysUserPo sysUserPo = SysUserPoConvert.INSTANCE.convertDtoToPo(sysUserDto);
        sysUserPo.insertFill();
        this.sysUserDao.insert(sysUserPo);
        return sysUserPo;
    }

    /**
     * 修改数据
     *
     * @param sysUserDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserPo update(SysUserDto sysUserDto) {
        SysUserPo sysUserPo = SysUserPoConvert.INSTANCE.convertDtoToPo(sysUserDto);
        sysUserPo.updateFill();
        this.sysUserDao.update(sysUserPo);
        return this.queryById(sysUserPo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysUserDao.deleteById(id) > 0;
    }

    /**
     * 通过主键逻辑删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean logicDeleteById(Long id) {
        return this.sysUserDao.logicDeleteById(id) > 0;
    }
}